package org.kongqian.gift.service.impl;


import org.kongqian.gift.domain.Spu;
import org.kongqian.gift.domain.SpuOnsaleDetail;
import org.kongqian.gift.exception.AssertionException;
import org.kongqian.gift.exception.code.ErrorCode;
import org.kongqian.gift.mapper.SpuMapper;
import org.kongqian.gift.mapper.SpuOnsaleDetailMapper;
import org.kongqian.gift.service.ISpuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * spu表 服务实现类
 * </p>
 *
 * @author kq
 * @since 2022-12-19
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements ISpuService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SpuOnsaleDetailMapper spuOnsaleDetailMapper;

    @Override
    @Transactional
    public void spuShelves(List<Long> ids) {
        //审核通过才能上架
        AssertionException.assertionListNotNull(ids, ErrorCode.ERROR_CODE_THE_PARAMETER_IS_INCORRECT);

        //查数据库判断用户选择的是不是都是审核通过的
        Integer num=spuMapper.selectByIds(ids);

        //判断用户选择上架的个数，和数据库查出来的个数相不相等
        AssertionException.assertionListNotEq(ids, num, ErrorCode.ERROR_CODE_THE_PARAMETER_IS_INCORRECT);

        //修改数据库状态
        spuMapper.updateByIds(ids);

        //添加上架数据到记录表由canal监听保存到es
        for (Long id : ids) {
            Spu spu = spuMapper.dataByIds(id);
            SpuOnsaleDetail spuOnsaleDetail = new SpuOnsaleDetail();
            spuOnsaleDetail.setName(spu.getGoodsName());
            spuOnsaleDetail.setMinPrice(spu.getLowPrice());
            spuOnsaleDetail.setMaxPrice(spu.getHighPrice());
            spuOnsaleDetailMapper.insert(spuOnsaleDetail);
        }

        //上架日志
    }

    @Override
    @Transactional
    public void spuTakeDown(List<Long> ids) {
        //修改数据库的状态
        //spuMapper.spuTakeDownupdateByIds(ids);        //删除同步数据表的数据

        spuOnsaleDetailMapper.deleteBatchIds(ids);
    }

}
